/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
	"context"
	"fmt"
	"gsc/types"
	"gsc/utils"
	"os"

	"github.com/hashicorp/go-getter/v2"
	"github.com/spf13/cobra"
)

var cbpActions = []types.UpdateFileAction{
	types.CreateUpdateFileAction("package.json", []types.ActionItem{
		{
			ActionItemType: 1,
			Replace:        "smvvm",
			Rewrite:        "${projectname}",
		},
	}, "package.json"),
}

// cbpCmd represents the cbp command
var cbpCmd = &cobra.Command{
	Use:   "cbp",
	Short: "create a bun project",
	Long:  `create a bun project`,
	Run: func(cmd *cobra.Command, args []string) {
		if len(args) == 0 {
			fmt.Println("Please provide a name for the project")
			return
		}

		projectName := args[0]
		projectNamePath := "./" + projectName

		e := os.RemoveAll(projectNamePath)
		if e != nil {
			fmt.Println("删除项目目录失败")
			return
		}

		// temp 目录下创建项目
		// 1. 创建项目目录
		e = os.MkdirAll(projectNamePath, os.ModePerm)
		if e != nil {
			fmt.Println("创建项目目录失败")
			return
		}

		client := getter.Client{}

		client.Get(context.Background(), &getter.Request{
			Src: "git::https://gitee.com/shusc24/bun_template.git",
			Dst: "./" + projectName,
		})

		utils.WalkConfigTo(cbpActions, projectName, ".")
	},
}

func init() {
	rootCmd.AddCommand(cbpCmd)

	// Here you will define your flags and configuration settings.

	// Cobra supports Persistent Flags which will work for this command
	// and all subcommands, e.g.:
	// cbpCmd.PersistentFlags().String("foo", "", "A help for foo")

	// Cobra supports local flags which will only run when this command
	// is called directly, e.g.:
	// cbpCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
